﻿#include <boost/lexical_cast.hpp>
#include "tplmgr.h"
#include "log.h"
#include "string_common.h"

CTPLManager::CTPLManager()
{
	m_account_id_set.clear();
	m_account_limit.clear();
}

CTPLManager::~CTPLManager()
{

}

bool CTPLManager::LoadTPL(bool reload /*= false*/)
{
	bool ret = LoadAccountLimit();
	if ( ret != true )
	{
		LOG_ERROR("default","LoadAccountLimit is err");
	}
	ret = LoadUnionInfo();
	if (ret != true)
	{
		LOG_ERROR("default","LoadUnionInfo is err");
	}
	if (LoadunionBossinfo() != true)
	{
		LOG_ERROR("default", "LoadunionBossinfo is err");
	}
	ret = activity_config_.LoadFromFile();
	if (ret != true)
	{
		LOG_ERROR("default","activity_config_.LoadFromFile is err");
	}
	if (LoadTeamBossInfo()!= true)
	{
		LOG_ERROR("default", "LoadTeamBossInfo is err");
	}
	if (LoadGlobalConfig() != true)
	{
		LOG_ERROR("default", "LoadGlobalConfig is error");
	}
	LoadUnionAlcohol();
	return true;
}

bool CTPLManager::LoadAccountLimit()
{
	m_account_id_set.clear();
	m_account_limit.clear();
	Json::Value _jv = ReadJsonFile("config/account_limit.json");
	if (_jv != Json::nullValue)
	{
		m_account_limit = _jv;
		uint32_t _count = (uint32_t)(m_account_limit.size());
		for (uint32_t i = 0; i < _count; ++i)
		{
			uint32_t accountid = m_account_limit[i].asUInt();
			auto _it = m_account_id_set.find(accountid);
			{
				if (_it == m_account_id_set.end())
				{
					m_account_id_set.insert(accountid);
				}
			}
		}
		return true;
	}
	else
	{
		LOG_ERROR("default","Load accountlimit error!");
		return false;
	}
}

bool CTPLManager::isLimitAccount(uint32_t account)
{
	auto _it = m_account_id_set.find(account);
	if (_it != m_account_id_set.end())
	{
		return true;
	}
	return false;
}

bool CTPLManager::LoadUnionInfo()
{
	m_union_info.clear();
	union_cfg.clear();
	Json::Value _jv = ReadJsonFile("data/map/guild.json");
	m_union_info = _jv;
	std::string jsonstr = JsonWriter(_jv);
	if (_jv != Json::nullValue)
	{
		int32_t count = m_union_info.size();
		for (int32_t i = 1; i <= count; ++i)
		{
			CUnionLvCfg lvcfg;
			std::string index = ::common::ToString(i);
			lvcfg.guild_exp = m_union_info[index]["guild_exp"].asInt();
			lvcfg.gold_contribution = m_union_info[index]["gold_contribution"].asInt();
			lvcfg.diamond_contribution = m_union_info[index]["diamond_contribution"].asInt();
			lvcfg.gold_sign = m_union_info[index]["gold_sign"].asInt();
			lvcfg.diamond_sign = m_union_info[index]["diamond_sign"].asInt();
			lvcfg.guild_shop = m_union_info[index]["guild_shop"].asInt();
			lvcfg.upper_limit = m_union_info[index]["upper_limit"].asInt();
			lvcfg.guild_boss_actid = m_union_info[index]["guild_boss"].asInt();
			lvcfg.guild_quit_cd = m_union_info[index]["guild_quit_cd"].asInt();
			lvcfg.alcohol_ID = m_union_info[index]["alcohol_ID"];

			Json::Value gold_reward = m_union_info[index]["gold_reward"];
			for (int32_t j = 0; j < gold_reward.size() && j < 2; ++j )
			{
				int32_t k = 0;
				lvcfg.gold_reward[j].itemid = gold_reward[j][k++].asInt();
				lvcfg.gold_reward[j].count = gold_reward[j][k++].asInt();
			}
			Json::Value diamond_reward = m_union_info[index]["diamond_reward"];
			for (int32_t j = 0; j < diamond_reward.size() && j < 2; ++j)
			{
				int32_t k = 0;
				lvcfg.diamond_reward[j].itemid = diamond_reward[j][k++].asInt();
				lvcfg.diamond_reward[j].count = diamond_reward[j][k++].asInt();
			}
			Json::Value expense = m_union_info[index]["expense"];
			for (int32_t l = 0; l < expense.size(); ++l)
			{
				int32_t k = 0;
				lvcfg.expense[l].begin_id = expense[l][k++].asInt();
				lvcfg.expense[l].end_id = expense[l][k++].asInt();
				lvcfg.expense[l].itemid = expense[l][k++].asInt();
				lvcfg.expense[l].count = expense[l][k++].asInt();
			}

			Json::Value need_gold = m_union_info[index]["need_gold"];
			int32_t k = 0;
			lvcfg.need_gold.itemid = need_gold[k++].asInt();
			lvcfg.need_gold.count = need_gold[k++].asInt();

			Json::Value need_diamond = m_union_info[index]["need_diamond"];
			k = 0;
			lvcfg.need_diamond.itemid = need_diamond[k++].asInt();
			lvcfg.need_diamond.count = need_diamond[k++].asInt();

			union_cfg.insert(std::make_pair(i, lvcfg));
		}
	}
	else
	{
		LOG_ERROR("default","load union donation table err");
		return false;
	}
	return true;
}

const CUnionLvCfg* CTPLManager::GetUnionCfgByLevel(int32_t union_level)
{
	auto it = union_cfg.find(union_level);
	if (it == union_cfg.end())
	{
		return NULL;
	}
	return &(it->second);
}

const CUnionAlcohol* CTPLManager::GetUnionAlcoholByID(int32_t alcohol_id)
{
	auto it = union_alcohol_map.find(alcohol_id);

	if (it == union_alcohol_map.end())
	{
		return NULL;
	}
	return &(it->second);
}

bool CTPLManager::LoadunionBossinfo()
{
	Json::Value _jv = ReadJsonFile("data/map/guildboss_monster.json");
	if (_jv != Json::nullValue)
	{
		m_UnionBossCfg.LoadFromJson(_jv);
	}
	else
	{
		LOG_ERROR("default", "load union donation table err");
		return false;
	}
	return true;
}

bool CTPLManager::LoadTeamBossInfo()
{
	Json::Value _jv = ReadJsonFile("data/map/teamBoss_monster.json");
	if (_jv != Json::nullValue)
	{
		team_boss_cfg_.LoadFromJson(_jv);
	}
	else
	{
		LOG_ERROR("default", "load union donation table err");
		return false;
	}
	return true;
}

bool CTPLManager::LoadUnionAlcohol()
{
	Json::Value _jv = ReadJsonFile("data/map/guild_alcohol.json");
	union_alcohol_map.clear();
	if (_jv != Json::nullValue)
	{
		int32_t size_arr = _jv.size();
		Json::Value::Members _tmp = _jv.getMemberNames();
		
		for (int32_t i = 0; i < size_arr; ++i)
		{
			CUnionAlcohol union_alcohol;
			std::string index = _tmp[i];

			union_alcohol.skill_id = _jv[index]["skill_id"].asInt();
			union_alcohol.name_alcohol = _jv[index]["name_alcohol"].asString();
			union_alcohol.icon_alcohol = _jv[index]["icon_alcohol"].asString();
			union_alcohol.con_time = _jv[index]["con_time"].asInt();
			union_alcohol.alcohol_id = atoi(index.c_str());
			int32_t idx = boost::lexical_cast< int32_t >( index );
			union_alcohol_map.insert(std::make_pair( idx, union_alcohol));
		}

		LOG_DEBUG("default", "CTPLManager::LoadUnionAlcohol [%d]", size_arr);
	}
	else {
		LOG_ERROR("default", "load union alcohol table error");
		return false;
	}

	return true;
}

bool CTPLManager::LoadGlobalConfig()
{
	global_config_.LoadFromFile();
	return true;
}